package com.teacherprofile.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teacherprofile.common.domain.SysUserDetails;
import com.teacherprofile.common.exception.ServiceException;
import com.teacherprofile.common.utils.SecurityUtils;
import com.teacherprofile.system.mapper.SysUserDetailsMapper;
import com.teacherprofile.system.service.ISysUserDetailsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class SysUserDetailsServiceImpl extends ServiceImpl<SysUserDetailsMapper, SysUserDetails> implements ISysUserDetailsService {

    @Override
    public SysUserDetails getUserDetails() {
        Long userId = SecurityUtils.getUserId(); //当前登录的用户id
        if (ObjectUtils.isNull(userId)) throw new ServiceException("获取用户信息失败");
        SysUserDetails userDetails = lambdaQuery().eq(SysUserDetails::getUserId, userId).one();
        return userDetails;
    }

    @Override
    public int insertSysUserDetails(SysUserDetails sysUserDetails) {
        return save(sysUserDetails) ? 1 : 0;
    }

    @Override
    public int updateSysUserDetails(SysUserDetails user) {
        if (ObjectUtils.isNull(user)) throw new ServiceException("非法参数");
        Long curId = SecurityUtils.getUserId();
        if (!curId.equals(user.getUserId()) && !SecurityUtils.isAdmin(curId)) {// 判断是否是本人
            throw new ServiceException("非法操作，不可修改他人数据！");
        }
        return lambdaUpdate()
                .eq(SysUserDetails::getUserId, user.getUserId())
                .update(user) ? 1 : 0;
    }

    /**
     * 批量插入详情信息
     * @param userDetailsList
     */
    @Override
    public void batchInsert(List<SysUserDetails> userDetailsList) {

    }
}
